package com.vast.ui.component.toolbar;

import org.eclipse.jface.action.Action;

import com.vast.ui.component.models.ItemArray;
import com.vast.ui.component.socketinterface.launchar.InterfaceLaunchar;
import com.vast.ui.itempanel.Activator;
import com.vast.ui.itempanel.views.ViewPart;

public class ToolBarAction extends AbstractToolBarAction {
	private final Action          startAction;
	private final Action          stopAction;
	private final Action          addAction;
	private final Action          deleteAction;
	
	public ToolBarAction(final ViewPart view, final InterfaceLaunchar serverLaunchar) {
		super(view);
		startAction = new Action() {
			public void run() {
				//openConnection();
				logger.writeEntry("startAction start");
				startAction.setEnabled(false);
				stopAction.setEnabled(true);
				serverLaunchar.openConnection();
			}
		};
		startAction.setEnabled(true);
		startAction.setToolTipText("Open Socket Connection");
		startAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("START_ENABLED"));
		
		stopAction = new Action() {
			public void run() {
				//closeConnection();
				logger.writeEntry("stopAction start\n");
				serverLaunchar.closeConnection();
				startAction.setEnabled(true);
				stopAction.setEnabled(false);
			}
		};
		stopAction.setEnabled(false);
		stopAction.setToolTipText("Close Socket Connection");
		stopAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("STOP_ENABLED"));
		
		addAction = new Action() {
			public void run() {
				logger.writeEntry("addAction selected\n");
				ItemArray itemArray = new ItemArray(8, 0);
				view.itemPanel.add(itemArray);
				view.display.asyncExec(new Runnable() {
					public void run() {
						view.viewer.setInput(view.itemPanel);
					}
				});
			}
		};
		addAction.setEnabled(true);
		addAction.setToolTipText("Add Item Raw");
		addAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("ADD"));
		
		deleteAction = new Action() {
			public void run() {
				logger.writeEntry("deleteAction selected]\n");
				ItemArray itemArray = view.itemPanel.getList().get(view.itemPanel.getList().size() - 1);
				view.itemPanel.remove(itemArray);
				view.display.asyncExec(new Runnable() {
					public void run() {
						view.viewer.setInput(view.itemPanel);
					}
				});
			}
		};
		deleteAction.setEnabled(true);
		deleteAction.setToolTipText("Delete");
		deleteAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("DELETE"));
		
		toolbar.add(startAction);
		toolbar.add(stopAction);
		toolbar.add(addAction);
		toolbar.add(deleteAction);
	}

}
